package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

import static com.example.tests.TestBase.*;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parametrs: <amount of test data> <file> <format>");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exist, please remove it manually: " + file);
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unknown format " + format);
		}
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstname() + "," + contact.getLastname() + "," + contact.getAddress() + "," 
			+ contact.getHome() + "," + contact.getMobile() + "," + contact.getWork() + "," + contact.getEmail() + ","
			+ contact.getEmail2() + "," + contact.getBday() + "," + contact.getBmonth() + "," + contact.getByear() + ","
			+ contact.getAddress2() + "," + contact.getPhone2() + ",!" + "\n");
		}
		writer.close();
		
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line!=null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
			.withFirstname(part[0])
			.withLastname(part[1])
			.withAddress(part[2])
			.withHome(part[3])
			.withMobile(part[4])
			.withWork(part[5])
			.withEmail(part[6])
			.withEmail2(part[7])
			.withBday(part[8])
			.withBmonth(part[9])
			.withByear(part[10])
			.withAddress2(part[11])
			.withPhone2(part[12]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	@SuppressWarnings("unchecked")
	public static List<ContactData> loadContactsFromXmlFile(File file)  throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			ContactData contact = new ContactData()
			.withFirstname(generateRandomString(20))
			.withLastname(generateRandomString(20))
			.withAddress(generateTextSymbols(50))
			.withHome(generateRandomStringWithNumbers(11))
			.withMobile(generateRandomStringWithNumbers(11))
			.withWork(generateRandomStringWithNumbers(11))
			.withEmail(generateRandomString(10) + "@test.ru")
			.withEmail2(generateRandomString(10) + "@test.ru")
			.withBdayBmonthByear(generateRandomDate())
			.withAddress2(generateTextSymbols(50))
			.withPhone2(generateRandomStringWithNumbers(11));
			list.add(contact);
		}
		return list;
	}
	
}
